import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/widget/appbar/CAppBar.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

class ImageViewerWidget extends StatefulWidget {

  final List<String> urls;
  final int index;
  const ImageViewerWidget(this.urls, {Key? key, this.index = 0}) : super(key: key);


  @override
  State<StatefulWidget> createState() {
    return ImageViewerWidgetState();
  }
}

class ImageViewerWidgetState extends BaseState<ImageViewerWidget> {
  late PageController pageController;

  @override
  void initState() {
    super.initState();
    pageController = PageController();;
  }
  @override
  Widget build(BuildContext context) {
    return
      Scaffold(
        // appBar:
        body:
          Stack(children: [
            Container(
              child: PhotoViewGallery.builder(
                scrollPhysics: const BouncingScrollPhysics(),
                builder: (BuildContext context, int index) {
                  return PhotoViewGalleryPageOptions(
                    imageProvider: LoadImage.getImageProvider(widget.urls[index]), //AssetImage(widget.urls[index]),
                    initialScale: PhotoViewComputedScale.contained * 1.0, //初始比例
                    // heroAttributes: PhotoViewHeroAttributes(tag: galleryItems[index].id),
                  );
                },
                itemCount: widget.urls.length,
                loadingBuilder: (context, event) => Center(
                  child: Container(
                    width: 20.0,
                    height: 20.0,
                    child: CircularProgressIndicator(
                      value: event == null
                          ? 0
                          : event.cumulativeBytesLoaded / (event.expectedTotalBytes ?? 1),
                    ),
                  ),
                ),
                backgroundDecoration: BoxDecoration(color: Colors.black),
                pageController: pageController,
                onPageChanged: onPageChanged,
              )
            ),
            CAppBar(bgColor: Colors.black,),
          ])
      );

  }

  onPageChanged(i) {

  }

}